package com.sporadic.backstage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sporadic.backstage.entity.Dishes;
import com.sporadic.backstage.service.DishesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import utils.Constant;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@CrossOrigin(origins = {"http://localhost:8080"}, methods = {RequestMethod.GET, RequestMethod.POST}, allowCredentials = "true")
@Controller
public class DishesController {
    @Autowired
    private DishesService dishesService;

    /**
     * 根据菜品名分页查询菜品信息
     * @param map 当前页 商家编号 菜品名
     * @return 返回IPage
     */
    @ResponseBody
    @RequestMapping("/selDishesByName")
    public IPage<Dishes> selDishesByName(@RequestBody Map<String,Object> map){
        Page<Dishes> page=new Page<>();
        page.setCurrent((Integer) map.get("current"));
        page.setSize((Integer) map.get("pageSize"));
        return dishesService.selDishesByDname(page, map);
    }

    /**
     * 添加菜品
     * @param dishes 菜品详情
     * @return 受影响行数
     */
    @ResponseBody
    @RequestMapping("/addDishes")
    public Integer addDishes(Dishes dishes){
        return dishesService.addDishes(dishes);
    }

    /**
     * 菜品图片上传
     * @param multipartFile 图片文件
     * @return 返回文件名
     */
    @PostMapping("/uploadFile")
    @ResponseBody
    public String uploadFile(@RequestParam("file") MultipartFile multipartFile,
                             @RequestParam("target") String target){
        //文件名
        String fileName = multipartFile.getOriginalFilename();
        //后缀名
        String lastFileName = fileName.substring(fileName.lastIndexOf("."));
        //生成文件名
        Date dd = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
        fileName = sf.format(dd).concat(lastFileName);
        //上传路径
        String uploadPath = Constant.NginxPath.CHANG_NGINX_PATH+target;
        //上传到项目内的文件对象
        File file = new File(uploadPath,fileName);

        File file1 = new File(uploadPath);
        try {
            if(!file1.exists()){
                //判建由此抽象路径名命名的目录。
                file1.mkdir();
            }
            //把数据传输到指定的文件中
            multipartFile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileName;
    }

    /**
     * 删除所上传的菜品土图片
     * @param imgName 图片名称
     * @return 返回上传结果
     */
    @GetMapping("/delDishesImage")
    @ResponseBody
    public String delDishesImage(@RequestParam("imgName") String imgName,
                                 @RequestParam("target") String target){
        System.out.println(target);
        String path = Constant.NginxPath.CHANG_NGINX_PATH+target;
        String result = null;
        String imgPath = path+"\\"+imgName;
        File file = new File(imgPath);
        //判断文件是否存在
        if(file.exists()){
            //如果存在 删除
            if(file.delete()){
                result = "1";
            }else{
                result = "0";
            }
        }else{
            result = "文件不再存在";
        }
        return result;
    }


    /**
     * 删除菜品
     * @param list 菜品编号
     * @return 返回受影响行数 大于0删除成功 否则失败
     */
    @ResponseBody
    @RequestMapping("/delDishesByID")
    public Integer delDishesByID(@RequestParam("list") List list){
        return dishesService.delDishesByID(list);
    }

    /**
     * 查询菜品种类总数
     * @param merchantId 商家编号
     * @return 菜品种类总数
     */
    @ResponseBody
    @RequestMapping("/selDishesCount")
    public Integer selDishesCount(Integer merchantId){
      return dishesService.dishesCount(merchantId);
    }

    /**
     * 查询低于指定库存的商品
     * @param map 商家编号 库存警告数量  当前页 页面大小
     * @return 低于指定库存商品详情
     */
    @ResponseBody
    @RequestMapping("/selByInventory_DishesCount")
    public IPage<Dishes> selByInventory_DishesCount(@RequestBody Map<String,Object> map){
        Page<Dishes> page=new Page<>();
        page.setCurrent((Integer) map.get("current"));
        page.setSize((Integer) map.get("pageSize"));
        return dishesService.selByInventory_DishesCount(page,map);
    }

    /**
     * 批量修改商品状态
     * @param list 商品ID集合
     * @return 修改结果
     */
    @ResponseBody
    @RequestMapping("/modifyDishesStatus")
    public Integer modifyDishesStatus(@RequestParam("list") List list){
        return dishesService.modifyDishesStatus(list);
    }

}
